package com.rabbit.eap.system.component;

import com.rabbit.eap.framework.shiro.vo.LoginSysRoleVO;
import com.rabbit.eap.framework.shiro.vo.LoginSysUserVO;
import com.rabbit.eap.system.convert.SysRoleConvert;
import com.rabbit.eap.system.convert.SysUserConvert;
import com.rabbit.eap.system.entity.SysDept;
import com.rabbit.eap.system.entity.SysMenu;
import com.rabbit.eap.system.entity.SysRole;
import com.rabbit.eap.system.entity.SysUser;
import com.rabbit.eap.system.service.ISysDeptService;
import com.rabbit.eap.system.service.ISysMenuService;
import com.rabbit.eap.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @projectName eap
 * @packageName com.rabbit.eap.system.component
 * @className SysPermissionService
 * @author： rabbit
 * @version： 1.0
 * @since： 2020/5/31 21:46
 * <p>Description: SysPermissionService
 * 用户权限处理
 * </p>

 */
@Component
public class SysPermissionService {

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * @Description 获取角色数据权限
     * @methodName: getRolePermission
     * @param user: 用户信息
     * @return: java.util.Set<java.lang.String> 角色权限信息
     * @date: 2020/5/31 22:03
     * @author: rabbit
     *
     */
    public Set<String> getRolePermission(SysUser user) {
        Set<String> roles = new HashSet<String>();
        Set<String> set =  roleService.selectRolePermissionByUserId(user.getId());
        roles.addAll(set);
        return roles;
    }

    /**
     * @Description 获取菜单数据权限
     * @methodName: getMenuPermission
     * @param user: 用户信息
     * @return: java.util.Set<java.lang.String>  菜单权限信息
     * @date: 2020/5/31 22:03
     * @author: rabbit
     *
     */
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> roles = new HashSet<String>();
        Set<String> set =menuService.selectMenuPermsByUserId(user.getId());
        roles.addAll(set);
        return roles;
    }
    /**
     * @Description 根据部门id获取部门
     * @methodName: getSysDeptById
     * @param id: 部门id
     * @return: com.rabbit.eap.system.entity.SysDept
     * @date: 2020/6/5 18:53
     * @author: rabbit
     *
     */
    public SysDept  getSysDeptById(Long  id){
       SysDept sysDept =  sysDeptService.getById(id);
       return sysDept;
    }
    /**
     * @Description: 获取用户的角色
     * @methodName: getRoles
     * @param user:
     * @return: java.util.List<com.rabbit.eap.system.entity.SysRole>
     * @date: 2020/9/22 21:07
     * @author: rabbit
     *
     */
    public List<SysRole> getRoles(SysUser user){
        List<SysRole> list = roleService.getRoles(user.getId());
        return list;
    }
    /**
     * @Description: 获取登录用户信息相关信息
     * @methodName: getLoginSysUser
     * @param sysUser:
     * @return: com.rabbit.eap.framework.shiro.vo.LoginSysUserVO
     * @date: 2020/9/22 21:07
     * @author: rabbit
     *
     */
    public LoginSysUserVO getLoginSysUser(SysUser sysUser){

        Set<String> roleSet =  getRolePermission(sysUser);

        Set<String> menuSet = getMenuPermission(sysUser);

        SysDept sysDept = getSysDeptById(sysUser.getDeptId());

        List<SysRole> sysRoleList  = getRoles(sysUser);
        List<LoginSysRoleVO> loginSysRoleVOList = SysRoleConvert.INSTANCE.sysRoleToLoginSysRole(sysRoleList);
        Long deptId  = null;
        String deptName =  null;
        if(!ObjectUtils.isEmpty(sysDept)){
            deptId = sysDept.getId();
            deptName = sysDept.getDeptName();
        }

        // 将系统用户对象转换成登录用户对象
        LoginSysUserVO loginSysUser = SysUserConvert.INSTANCE.sysUserToLoginSysUserVO(sysUser);
        loginSysUser.setRoleIds(roleSet);
        loginSysUser.setPermissions(menuSet);
        loginSysUser.setDeptId(deptId);
        loginSysUser.setDeptName(deptName);
        loginSysUser.setRoles(loginSysRoleVOList);
        return loginSysUser;
    }
    public List<SysMenu> getUserMenu( SysUser sysUser) {
        return menuService.selectMenusByUser(sysUser);
    }
}
